<script language="javascript">
	function viewRaoVatByCat(CatID){
		window.location='?mod=classified.listclassified&catid=' + CatID;
	}
	function viewRaoVatByLocation(LID){
		window.location='?mod=classified.listclassified&location=' + LID;
	}
	function viewRaoVatByStatus(SID){
		window.location='?mod=classified.listclassified&act=' + SID;
	}
	function viewRaoVatByHOT(HID){
		window.location='?mod=classified.listclassified&vip=' + HID;
	}
	function viewRaoVatByOrder(Order){
		window.location='?mod=classified.listclassified&order=' + Order;
	}
</script>
<?
if(isset($_GET['method']))
{
	$method = $_GET['method'];
	if($method=="del"){
		$sListId="";
		if(isset($_POST["chon"]))$sListId=$_POST["chon"];
		$sql="delete from ".$prefix."raovat where rv_id in('".str_replace(",","','",$sListId)."')";
		mysql_query($sql);
		Redirect("Bạn đã xoá thành công","?mod=classified.listclassified");
	}else if($method=="act"){
		$IdList=$_GET["id"];
		$act=$_GET["act"];
		if($act==1) $iact=0;else $iact=1;
		$sql="update ".$prefix."raovat set rv_hienthi=$iact where rv_id=$IdList";
		mysql_query($sql);
		Redirect("","?mod=classified.listclassified");		
	}else if($method=="delete"){
		$IdList=$_GET["id"];
		$sql="delete from ".$prefix."raovat where rv_id=$IdList";
		mysql_query($sql);
		Redirect("Đã xóa thành công","?mod=classified.listclassified");		
	}else if($method=="vip"){
		$IdList=$_GET["id"];
		$vip=$_GET["vip"];
		if($vip==1) $ivip=0;else $ivip=1;
		$sql="update ".$prefix."raovat set rv_vip=$ivip where rv_id=$IdList";
		mysql_query($sql);
		Redirect("","?mod=classified.listclassified");		
	}	
}
$where = " WHERE 1";
$order ="";
if(isset($_GET['catid']))
{
	$catid = $_GET['catid'];
	$where .=" AND rv_loairaovat=$catid";
}else{
	$catid = '-1';
}

if(isset($_GET['location'])){
	$location = $_GET['location'];
	$where .=" AND rv_tinhthanh=$location";
}else{
	$location = '-1';
}


if(isset($_GET['act'])){
	$act = $_GET['act'];
	$where .= " AND rv_hienthi=$act";
}else{
	$act = 3;
} 

if(isset($_GET['vip'])){
	$vip = $_GET['vip'];
	$where .= " AND rv_vip=$vip";
}else{
	$vip = 3;
} 

if(isset($_GET['order'])){
	$iorder = $_GET['order'];
	$where = $where;
	if($iorder=="DESC"){
		$order .= " order by rv_thoigian DESC";
	}else{
		$order .= " order by rv_thoigian ASC";
	}
}else{
	$where = $where;
	$order .= " order by rv_thoigian DESC";
	$iorder = 3;
}
	///////Paging
	$strSQL = "select * from ".$prefix."raovat".$where.$order."";
	//echo $strSQL;
	$pp=10; 
	$parr=array();
	$parr=paging($strSQL,$pp);
	$page=$parr[0];
	$limitvalue=$parr[1];
	$numofpages=$parr[2];
	$strSQL.=" LIMIT $limitvalue, $pp";
	$rs = mysql_query($strSQL) or die('Not query');
	$i=0;
	$isexits=false;
	
	if(mysql_num_rows($rs)) $isexits=true;
?>
<div class="newsList-path"></div>
	<div style="margin-bottom:5px; font-size:14px;">
	<?
	$sql = "select count(*) as total from ".$prefix."raovat".$where."";
	$result = mysql_query($sql) or die("Can't connect database");
	$row = mysql_fetch_array($result);
	?>
	 Có <?=$row['total']?> tin rao vặt được tìm thấy </div>
	<div align="right" class="newsList-search-up">
	<strong>Lọc tin </strong>&nbsp;
	<select name="FilterByCat" id="CatID" onchange="return(viewRaoVatByCat(this.value));">
	<option value="-1" <? if($catid == '-1'){ ?> selected="selected" <? } ?>>--Danh mục--</option>
	<?
	$sql = "select * from ".$prefix."loairaovat order by rvc_id ASC";
	$result = mysql_query($sql) or die("Can't connect database");
	while($row = mysql_fetch_array($result))
	{
	?>	
	<option value="<?=$row['rvc_id']?>" <? if($catid == $row['rvc_id']){ ?> selected="selected" <? } ?>><?=$row['rvc_ten']?></option>
	<? }?>
	  </select>
	  &nbsp;<select name="FilterByTinhThanh" id="LID" onchange="return(viewRaoVatByLocation(this.value));">
	<option value="-1" <? if($location == '-1'){ ?> selected="selected" <? } ?>>--Khu vực--</option>
	<?
	$sql = "select * from ".$prefix."tinhthanh order by tt_ten ASC";
	$result = mysql_query($sql) or die("Can't connect database");
	while($row = mysql_fetch_array($result))
	{
	?>	
	<option value="<?=$row['tt_id']?>" <? if($location == $row['tt_id']){ ?> selected="selected" <? } ?>><?=$row['tt_ten']?></option>
	<? }?>	
	  </select>
	  &nbsp;<select name="FilterByStatus" id="SID" onchange="return(viewRaoVatByStatus(this.value));">
	<option value="3" <? if($act == 3){ ?> selected="selected" <? } ?>>--Trạng thái--</option>
	<option value="1" <? if($act == 1){ ?> selected="selected" <? } ?>>Đã kích hoạt</option>
	<option value="0" <? if($act == 0){ ?> selected="selected" <? } ?>>Chưa kích hoạt</option>
	  </select>
	   &nbsp;<select name="FilterByHOT" id="HID" onchange="return(viewRaoVatByHOT(this.value));">
	<option value="3" <? if($vip == 3){ ?> selected="selected" <? } ?>>----Tất cả----</option>
	<option value="1" <? if($vip == 1){ ?> selected="selected" <? } ?>>VIP</option>
	<option value="0" <? if($vip == 0){ ?> selected="selected" <? } ?>>Khác</option>
	  </select>
	  &nbsp;<select name="FilterByOrder" id="Order" onchange="return(viewRaoVatByOrder(this.value));">
	<option value="3" <? if($iorder == 3){ ?> selected="selected" <? } ?>>--Sắp xếp--</option>
	<option value="ASC" <? if($iorder == 'ASC'){ ?> selected="selected" <? } ?>>Mới nhất</option>
	<option value="DESC"<? if($iorder == 'DESC'){ ?> selected="selected" <? } ?>>Cũ nhất</option>
	  </select>
	</div>
	<form name="frmListSp" method="post" action="?mod=classified.listclassified&method=del" onSubmit="return checkInput(this,'Bạn hãy chọn ít nhất một giá trị','Bạn chắc chắn muốn xoá không');">
	<input type="hidden" name="chon" />
	<table width="100%" border="1" bordercolor="#B2DAEE" cellpadding="4">
		<tr class="newsList-content-head">
		<td width="4%" class="newsList-content-view"><input type="checkbox" name="chkall" value="0" <? if($isexits) echo "onClick=\"docheck(this.form,0);\""?>></td>
			<td width="41%" class="newsList-content-title">Tiêu đề</td>

			<td width="7%" class="newsList-content-view"><div align="center">Hiển thị </div></td>
			<td width="7%" class="newsList-content-view"><div align="center">VIP </div></td>
			<td width="17%" class="newsList-content-view"><div align="center">Loại rao vặt </div></td>
			<td width="9%" class="newsList-content-position">Khu vực</td>
			<td width="15%" class="newsList-content-position">Tùy chọn </td>
		</tr>
				<?
				while($rows = mysql_fetch_array($rs))
				{
					$i++;
					$rv_id = $rows['rv_id'];
					$rv_loairaovat = $rows['rv_loairaovat'];
					$rv_nguoidang = $rows['rv_nguoidang'];
					$rv_tieude = $rows['rv_tieude'];
					$rv_hinhanh = $rows['rv_anhthehien'];
					$rv_tinhthanh = $rows['rv_tinhthanh'];
					$rv_thoigian = $rows['rv_thoigian'];
					$rv_solanxem = $rows['rv_solanxem'];
					$rv_vip = $rows['rv_vip'];
					$rv_hienthi = $rows['rv_hienthi'];
				?>				
		<tr onmouseover="this.bgColor='#FEF2CB';" onmouseout="this.bgColor='#FFFFFF';" class="newsList-content-list" <? if($i%2==0) echo "bgcolor='#f2fff2'";?>>
			<td class="newsList-content-title"><input type="checkbox" name="Xoa" value="<?=$rv_id?>" onClick="docheckone(this.form);"></td>
			<td class="newsList-content-title">
            	<table cellpadding="0" cellspacing="0" width="100%">
                <tr>
					<td colspan="2">
						<a class="newsList-link newsList-name" href="">
						<b><?=$rv_tieude?></b></a>&nbsp;<i>(Xem:<?=$rv_solanxem?>)</i>					</td>
                <tr>
                    <td><div class="newsList-user-id" >Người đăng: <a class="newslist-normal-link" href="#"><?=tenNguoiDung($rv_nguoidang);?></a></div></td>
                	<td align="right"><div class="newsList-time">Ngày đăng: <span class="newsList-date"><?=format_date($rv_thoigian);?></span></div></td>
                </tr>
                </table>				</td>
			<td class="newsList-content-view"><div align="center"><?=ImgActive($rv_hienthi);?></div></td>
			<td class="newsList-content-view"><div align="center"><?=ImgActive($rv_vip);?></div></td>
			<td class="newsList-content-view"><div align="center"><?=getLoaiRaoVat($rv_loairaovat);?></div></td>
			<td class="newsList-content-position"><a class="newsList-link" href="?mod=classified.listclassified&amp;location=<?=$rv_tinhthanh?>&amp;portal=<?=$mod?>">
			  <?=layTinhThanh($rv_tinhthanh);?>
			  </a></td>
			<td class="newsList-content-position"><div align="center"><a href="?mod=classified.edit&amp;id=<?=$rv_id?>" class="menu"><img src="images/edit.gif" border="0" /></a>&nbsp; <a href="?mod=classified.listclassified&amp;method=delete&amp;id=<?=$rv_id?>" class="menu" onclick="return confirm('Bạn có chắc chắn muốn xóa không?')"><img src="images/delete_on.gif" border="0" /></a></div></td>
		</tr>
		<? }?>
	<tr>
   <td colspan="9" align="center">
   <input type="submit" name="submit" value="   Xoá   " class="Button">
   <input type="reset" value="Làm lại" class="Button">   </td>
   </tr>
</table>
	</form>
	<div class="newsList-pagging">
	<div class="raovat-paging-bound">
	<span style="padding:5px;">
	<p align="center" class="font">
			<?php echo page_div( "?mod=classified.listclassified&p=%d_pg", "10", ceil ( $numofpages ), $page ); ?>
	</p>
	</span>
	</div>
	</div>
	<div class="newsList-button-back"><a href="javascript: history.back()">[Quay lại]</a></div>